'''
西宁市气温曲线图
'''
import json
import requests
from bs4 import BeautifulSoup
import re
import pyecharts.options as opts
from pyecharts.charts import Line
from pyecharts.globals import ThemeType

# 请求 URL
url1 = 'http://www.weather.com.cn/weather/101150101.shtml'
url2 = 'http://www.weather.com.cn/weather/101150102.shtml'
# 发送请求
response1 = requests.get(url1)
response1.encoding = 'utf-8'
response2 = requests.get(url2)
response2.encoding = 'utf-8'
# 解析 HTML
soup1 = BeautifulSoup(response1.text, 'lxml')
soup2 = BeautifulSoup(response2.text, 'lxml')
# 获取天气预报信息
weather = soup1.find('p', {'class': 'wea'}).text
temperature = soup1.find('p', {'class': 'tem'}).text
scripts = soup1.find_all('script')
for script in scripts:
    if 'hour3data' in script.text:
        json_str = re.search(r'hour3data=\{.*\}', script.text).group()
        json_str = json_str.split('=')[1]
# 使用 json 模块来解析字符串
data = json.loads(json_str)
# 提取"1d"键的值，即数组部分
array_data = data['1d']
# ----------
weather2 = soup2.find('p', {'class': 'wea'}).text
temperature2 = soup2.find('p', {'class': 'tem'}).text
scripts2 = soup2.find_all('script')
for script in scripts2:
    if 'hour3data' in script.text:
        json_str = re.search(r'hour3data=\{.*\}', script.text).group()
        json_str = json_str.split('=')[1]
# 使用 json 模块来解析字符串
data2 = json.loads(json_str)
# 提取"1d"键的值，即数组部分
array_data2 = data2['1d']
xaxis = []
yaxis_1 = []
yaxis_2 = []
for item in array_data:
    match = re.search(r'\d+日\d+时', item)
    match2 = re.search(r',(-?\d+)℃', item)
    temperature = int(match2.group(1))  # group(1) 是第一个捕获组的内容
    xaxis.append(match.group())
    yaxis_1.append(temperature)
for item in array_data2:
    match2 = re.search(r',(-?\d+)℃', item)
    temperature = int(match2.group(1))  # group(1) 是第一个捕获组的内容
    yaxis_2.append(temperature)

print("开始绘制西宁市气温曲线图")
line = (
    Line(opts.InitOpts(theme=ThemeType.DARK,height="300px",width="400px",chart_id='line',bg_color="rgba(16, 12, 42,0.8)"))
    .set_global_opts(title_opts=opts.TitleOpts(title='西宁市气温图',
                                               subtitle='Temperature map of Xining City',
                                               title_textstyle_opts=opts.TextStyleOpts(
                                                   font_size=15,  # 标题的字体大小
                                                   font_family='微软雅黑',
                                               ),
                                               pos_left='center',
                                               pos_top='15px'))
    .add_xaxis(xaxis)
    .add_yaxis("西宁市气温", yaxis_1, is_smooth=True)
    .add_yaxis("城北区气温", yaxis_2, is_smooth=True)
    #.add_yaxis("商家B", Faker.values(), is_smooth=True)
    .set_series_opts(
        areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
        label_opts=opts.LabelOpts(is_show=False),
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(),
        xaxis_opts=opts.AxisOpts(
            axistick_opts=opts.AxisTickOpts(is_align_with_label=True),
            is_scale=False,
            boundary_gap=False,
        ),
    )
)
line.render("5.曲线图.html")